<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of customer
 *
 * @author user
 */
class customer extends record {

    function __construct() {
        parent::__construct();
        $this->namaTabel = 'customer';
    }
    function getByID($id){
        $this->get('*', array('id'=>$id), 1);
        $e = $this->execute();
        return $e['result'][0];
    }
    function register($arrUser) {

    }
    function doLogin($username,$pass){
        $pas = sha1($pass);
        $this->get('*', array('password'=>$pas),1, null, null,null,'( username="'.$username.'" OR email="'.$username.'")');
        $ret = $this->execute();
        if($ret['success']==true){
            $dtResult  =isset($ret['result'][0])?$ret['result'][0]:array('aktif'=>999);
            if ($dtResult['aktif']==2){
                $ret=$dtResult;
                $ret['success']=false;
                $ret['aktif']=2;
                $ret['error_msg']='User Belum Melakukan Aktifasi.';
            }
            else if($ret['total']>0){
                $_SESSION['userlogin']['id']=$ret['result'][0]['id'];
                $_SESSION['userlogin']['nama']=$ret['result'][0]['nama'];
                $_SESSION['userlogin']['username']=$ret['result'][0]['username'];
                $_SESSION['userlogin']['email']=$ret['result'][0]['email'];
                $_SESSION['userlogin']['alamat']=$ret['result'][0]['alamat'];
                $_SESSION['userlogin']['kota']=$ret['result'][0]['kota'];
                $_SESSION['userlogin']['telepon']=$ret['result'][0]['telepon'];           
            }
            else{
                $ret['success']=false;
                $ret['error_msg']='Username atau Password salah';
            } 
        };
        return $ret;
    }
    function logout(){
        unset($_SESSION['userlogin']);
        unset($_SESSION['detail_cart']);
        return array('success'=>true);
    }
    function getData($limit,$start){
        $strW ="(NOT(aktif)='2')";
        $arWh=array();
        $this->get('*', $arWh, $limit, $start, 
        'waktu', 'DESC',$strW);
        //echo $this->strQuery;
        $ex= $this->execute();
        $result = isset($ex['result'])?$ex['result']: array( );
        $this->num_rows($arWh,array(),$strW);
        $tot = $this->execute();
        $total= isset($tot['result'])?$tot['result'][0]['total']:0;
        return array('result' =>$result ,'total'=>$total);        
    }
    function sendMail($email, $kode, $user) {
        $subject="Aktifasi Akun - ".MAINSITENAME;
        $message = '<html>
        <head>
        <title>Aktifasi Email</title>
        </head>
        <body>
        <h4>Kode Aktifasi Anda: </h4>
        <h1>' . $kode . '</h1>
        <p>Klik 
            <a href="' . BASEURL . 'proses/pelanggan.php?on=directActivation&username=' . $user . '&email=' . $email . '&a='.  sha1($kode).'">
                <h3>Disini</h3>
            </a> Untuk aktifasi akun Anda.</p>
        <p>Atau 
        <a href="'.BASEURL.'?v=activateuser&user=' . $user. '&email=' . $email.">
            <h3>Masukkan Kode Aktifasi Disini</h3>
        </a> jika Anda link diatas tidak bisa.</p>
        </body>
        </html>";
        $headers = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
        $headers .= 'To: <' . $email . '>' . "\r\n";
        $headers .= 'From: '.MAINSITENAME.' <' . MAILUTAMA . '>' . "\r\n";
        mail($email, $subject, $message, $headers);
    }
    function meanStatus($v){
        switch ($v) {
            case '0':
                $k = "NonAktif";
                break;
            case '1':
                $k = "Aktif";
                break;
                case '2':
                    $k = "Belum Konfirmasi Email";
                    break;
            default:
                $k = "NaN";
                break;
        }
        return $k;
    }

}

 $objCustomer = new customer();